package org.nvframe.manager;

import java.util.HashMap;
import java.util.Map;

import org.newdawn.slick.Sound;
import org.nvframe.event.eventtype.SoundEvent;
import org.nvframe.event.eventtype.SoundListener;
import org.nvframe.exception.NVFrameException;


/**
 * 
 * @author Nik Van Looy
 */
public class SoundManager implements SoundListener {
	
	private static final SoundManager _instance = new SoundManager();

	public static SoundManager getInstance() {
		return _instance;
	}
	
	private Map<String, Sound> sounds;

	private SoundManager() {
		sounds = new HashMap<String, Sound> ();
	}
	
	/**
	 * Add a Sound resource to the Manager
	 * 
	 * @param id the identifier (name from resources xml)
	 * @param sound the Sound resource
	 */
	public void addSound(String id, Sound sound) {
		sounds.put(id, sound);
	}
	
	/**
	 * Plays a sound with a given id
	 * 
	 * @param id the soundId
	 */
	public void playSound(String id) {
		if(!sounds.containsKey(id))
			return;
		Sound sound = sounds.get(id);
		sound.play();
	}
	
	@Override
	public void playSound(SoundEvent event) throws NVFrameException {
		if(!sounds.containsKey(event.getSoundId()))
			throw new NVFrameException("sound [id: " + event.getSoundId() + "] not found in SoundManager");
		Sound sound = sounds.get(event.getSoundId());
		sound.play();
	}

}
